package com.levi.juc.pool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shentong
 * @since 2023/2/10 22:09
 */
public class NamingThreadFactory implements ThreadFactory {
    private final String name;
    private final AtomicInteger threadNum = new AtomicInteger();

    public NamingThreadFactory(String name) {
        this.name = name;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(name + " [#" +threadNum.incrementAndGet()+"]");
        return thread;
    }
}
